package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.astrob.activitys.AppContext;
import com.astrob.activitys.DMapActivity;
import com.astrob.model.LonLat;
import com.astrob.model.Msg;
import com.astrob.naviframe.Start;
import com.astrob.nhcmap.R;
import com.astrob.util.SysUtils;
import com.astrob.util.Utils;
import com.astrob.view.BaseViewHolder;
import com.nhc.database.dbmodel.Route;
import com.nhc.database.dbmodel.WayPoint;
import com.nhc.database.manager.DBDataCenter;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.base.BaseAdapter;
import zuo.biao.library.model.Entry;
import zuo.biao.library.ui.AlertDialog;
import zuo.biao.library.ui.AlertDialog.OnDialogButtonClickListener;
import zuo.biao.library.ui.EditTextInfoActivity;
import zuo.biao.library.ui.EditTextInfoWindow;
import zuo.biao.library.ui.ItemDialog;
import zuo.biao.library.ui.PageScroller;
import zuo.biao.library.ui.TopMenuWindow;
import zuo.biao.library.util.StringUtil;

/**
 * 使用方法：复制>粘贴>改名>改代码
 */

/**
 * activity示例
 *
 * @warn 复制到其它工程内使用时务必修改import R文件路径为所在应用包名
 * @use toActivity(DemoActivity.createIntent(...));
 */
public class NaviLineDetailActivity extends BaseActivity implements OnDialogButtonClickListener, ItemDialog.OnDialogItemClickListener {
    private final String TAG = "NaviLineDetailActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, NaviLineDetailActivity.class);
    }
    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //TODO demo_activity改为你所需要的layout文件；传this是为了底部左右滑动手势
        setContentView(R.layout.navilinedetail_activity);

        mRoute = DBDataCenter.getInstance().routeEdit;
        if (mRoute == null) {
            finish();
        }
        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>
    }


    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    private ListView lvNavilineWp;
    private Button bt_do;
    private Route mRoute;
    private Button ctrl_navilinedetail_add;
    private ImageView lv_topbarMenu;//编辑模式都需要隐藏menu

    TextView tv_distance;
    TextView tv_area;

    //示例代码>>>>>>>>
    @Override
    public void initView() {//必须在onCreate方法内调用
        autoSetTitle();//自动设置标题为上个Activity传入的INTENT_TITLE
        lvNavilineWp = (ListView) findViewById(R.id.lvNavilineWp);
        bt_do = (Button) findViewById(R.id.bt_do);
        ctrl_navilinedetail_add = (Button) findViewById(R.id.ctrl_navilinedetail_add);
        tvBaseTitle.setText(mRoute.getName());

        lv_topbarMenu = (ImageView) findViewById(R.id.lv_topbarMenu);

        tv_distance = (TextView) findViewById(R.id.tv_distance);
        tv_area = (TextView) findViewById(R.id.tv_area);

        resetInfo();
    }

    void resetInfo() {
        ArrayList<LonLat> points = new ArrayList<LonLat>();
        for (int i = 0; i < mRoute.waypointList.size(); i++) {
            points.add(mRoute.waypointList.get(i).getLL());
        }
        double len = Utils.calculateLen(points);
        double area = Utils.calculateArea(points);

        String SS[] = SysUtils.getInstance().getDistanceStr((int) len);
        tv_distance.setText(getString(R.string.jl) + ": " + SS[0] + SS[1]);

        String AA[] = SysUtils.getInstance().getAreaStr(area);
        tv_area.setText(getString(R.string.mj) + ": " + AA[0] + AA[1]);
    }


    //示例代码<<<<<<<<
    private DemoAdapter adapter;
    //示例代码>>>>>>>>

    /**
     * 示例方法 ：显示列表内容
     *
     * @param list
     */
    private void setList(List<Entry<Integer, String>> list) {
        if (adapter == null) {
            adapter = new DemoAdapter(context);
            lvNavilineWp.setAdapter(adapter);
        }
        adapter.refresh(list);
        ctrl_navilinedetail_add.setText(getString(R.string.addnavipt) + list.size() + "/" + Route.MAX_WAYPOINT + "）");

        resetInfo();
    }
    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    private List<Entry<Integer, String>> navilinelist;

    private boolean mIsEditB = false;//调整顺序
    private boolean mIsEdit = false;//选择删除
    private HashMap<Integer, Boolean> hashMap;

    private boolean getItemChecked(int position) {
        return hashMap.get(position);
    }

    private void setItemChecked(int position, boolean isChecked) {
        if (adapter != null) {
            hashMap.put(position, isChecked);
            adapter.notifyDataSetChanged();
        }
    }

    //示例代码>>>>>>>>>
    private void resetRouteWp() {
        mRoute.waypointList = DBDataCenter.getInstance().getWaypointListByRoute(mRoute);
    }

    @Override
    public void initData() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        showProgressDialog(R.string.loading);
        runThread(TAG + "initData", new Runnable() {
            @Override
            public void run() {
                resetRouteWp();
                navilinelist = getList();
                runUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismissProgressDialog();
                        setList(navilinelist);
                    }
                });
            }
        });
        //示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }

    private int avilineNum = 0;

    protected List<Entry<Integer, String>> getList() {
        navilinelist = new ArrayList<Entry<Integer, String>>();
        for (int i = 0; i < mRoute.waypointList.size(); i++) {
            navilinelist.add(new Entry<Integer, String>(i, ""));
        }
        avilineNum = navilinelist.size();
        return navilinelist;
    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<
        lvNavilineWp.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mIsEdit) {
                    if (null != hashMap) {
                        boolean isSelected = hashMap.get(position);
                        hashMap.put(position, !isSelected);
                        if (adapter != null) {
                            adapter.notifyDataSetChanged();
                        }
                        boolean haveselected = false;
                        for (int i = 0; i < mRoute.waypointList.size(); i++) {
                            if (hashMap.get(i)) {
                                haveselected = true;
                                break;
                            }
                        }
                        if (haveselected) {
                            bt_do.setText(getString(R.string.delete));
                        } else {
                            bt_do.setText(getString(R.string.cancel));
                        }
                    }

                } else if (mIsEditB) {
                    onListOrder(position);
                } else {
                    onShowRouteWp(position);
                }
            }
        });

        //分页滚动示例代码<<<<<<<<<<<<<<<<<<<
        new PageScroller(lvNavilineWp).init();
        //分页滚动示例代码>>>>>>>>>>>>>>>>>>>

        //示例代码>>>>>>>>>>>>>>>>>>>
    }

    int mOrderIndex = -1;
    private void onListOrder(int position) {
        if (position < 0 || position >= navilinelist.size()) {
            showShortToast(getString(R.string.xzwx));
        }
        mOrderIndex = position;

        new ItemDialog(context, getResources().getStringArray(R.array.top_menu_navilinedetail_changeorder), "", DIALOG_SET_TOPBAR, this).show();
//        toActivity(ListOrderCtrlActivity.createIntent(this, mOrderIndex, navilinelist.size()), REQUEST_TO_LISTORDER);
    }

    private void onEditBNext(int position) {
        int newOrder = mOrderIndex;
        switch (position) {
            default:
                break;
            case 0:
                newOrder--;
                break;
            case 1:
                newOrder++;
                break;
            case 2:
                newOrder = 0;
                break;
            case 3:
                newOrder = navilinelist.size() - 1;
                break;
            case 4: {
                Intent intent = EditNumberActivity.createIntent(this, getString(R.string.inputorder), newOrder + 1, "");
                startActivityForResult(intent, REQUEST_TO_EDIT_ORDER);
                return;
            }
        }
        changeOrderList(newOrder);
    }

    private void changeOrderList(int newOrder) {
        if (newOrder < 0) {
            return;
        }
        if (newOrder >= navilinelist.size()) {
            newOrder = navilinelist.size() - 1;
        }
        if (mOrderIndex == newOrder) {
            showShortToast(getString(R.string.xtsxwxtz));
            return;
        }

        Entry<Integer, String> dataA = navilinelist.get(mOrderIndex);
        navilinelist.remove(mOrderIndex);
        navilinelist.add(newOrder, dataA);
        adapter.refresh(navilinelist);

        resetInfo();
    }


    private void onShowRouteWp(int index) {
        if (index < 0 || index >= mRoute.waypointList.size()) {
            return;
        }

        DBDataCenter.getInstance().makerEdit = mRoute.waypointList.get(index);
        toActivity(MarkerPointActivity.createIntent(getBaseContext(), false, true), REQUEST_DETAIL);
    }

    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //@user 新增方法
    @Override
    public void onForwardClick(View v) {
        toActivity(TopMenuWindow.createIntent(context, getResources().getStringArray(R.array.navilinedetail_menu)), REQUEST_TO_TOP_MENU, false);


    }

    public void onSelectAll(View v) {
        selectAll(true);
    }

    public void onSelectAllNO(View v) {
        selectAll(false);
    }

    public void selectAll(boolean isSelected) {
        if (hashMap == null) {
            hashMap = new HashMap<Integer, Boolean>();
        }
        hashMap.clear();
        if (navilinelist != null) {
            for (int i = 0; i < navilinelist.size(); i++) {
                hashMap.put(i, isSelected);
            }
        }
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }

        if (isSelected) {
            bt_do.setText(getString(R.string.delete));
        } else {
            bt_do.setText(getString(R.string.cancel));
        }
    }

    public void onDelSelected(View v) {

        boolean haveselected = false;
        for (int i = 0; i < mRoute.waypointList.size(); i++) {
            if (hashMap.get(i)) {
                haveselected = true;
                break;
            }
        }

        if (haveselected) {
            new AlertDialog(context, getString(R.string.wxts), getString(R.string.qdscxzdhd), true, 0, this).show();
        } else {
            mIsEdit = false;
            findViewById(R.id.ctrl_navilinedetail_edit).setVisibility(View.GONE);
            ctrl_navilinedetail_add.setVisibility(View.VISIBLE);
            lv_topbarMenu.setVisibility(View.VISIBLE);

            if (adapter != null) {
                adapter.notifyDataSetChanged();
            }
        }

    }

    private void delSelected() {
        mIsEdit = false;
        findViewById(R.id.ctrl_navilinedetail_edit).setVisibility(View.GONE);
        lv_topbarMenu.setVisibility(View.VISIBLE);
        ctrl_navilinedetail_add.setVisibility(View.VISIBLE);
        showShortToast(getString(R.string.deleteok));

        ArrayList<Long> pkIds = new ArrayList<Long>();

        for (int i = 0; i < mRoute.waypointList.size(); i++) {
            if (hashMap.get(i)) {
                pkIds.add(mRoute.waypointList.get(i).getId());
            }
        }
        DBDataCenter.getInstance().delWaypointOnRoute(mRoute, pkIds);
        initData();
    }

    private void clearListData() {
        ArrayList<Long> pkIds = new ArrayList<Long>();
        for (int i = 0; i < mRoute.waypointList.size(); i++) {
            pkIds.add(mRoute.waypointList.get(i).getId());
        }
        DBDataCenter.getInstance().delWaypointOnRoute(mRoute, pkIds);
        initData();
    }

    private void onToEdit() {
        if (navilinelist.size() < 1) {
            showShortToast(getString(R.string.tnotrackdelete));
            return;
        }

        lv_topbarMenu.setVisibility(View.GONE);
        mIsEdit = true;
        findViewById(R.id.ctrl_navilinedetail_edit).setVisibility(View.VISIBLE);
        ctrl_navilinedetail_add.setVisibility(View.GONE);

        selectAll(false);
    }

    private void onToEditB() {
        if (navilinelist.size() < 2) {
            showShortToast(getString(R.string.zslghdcntzsx));
            return;
        }

        showShortToast(getString(R.string.qdxhdtzsx));

        lv_topbarMenu.setVisibility(View.GONE);
        mIsEditB = true;
        findViewById(R.id.ctrl_navilinedetail_order).setVisibility(View.VISIBLE);
        ctrl_navilinedetail_add.setVisibility(View.GONE);
    }

    public void onOrderConfirm(View v) {
        String waypointsNew = "";
        for (int i = 0; i < navilinelist.size(); i++) {
            Entry<Integer, String> data = navilinelist.get(i);
            int index = data.getKey();
            Long wpId = mRoute.waypointList.get(index).getId();
            if (i > 0) {
                waypointsNew += "-" + wpId;
            } else {
                waypointsNew += wpId + "";
            }
        }

        mRoute.setWaypoint(waypointsNew);
        DBDataCenter.getInstance().saveRoute(mRoute);
        initData();

        mIsEditB = false;
        findViewById(R.id.ctrl_navilinedetail_order).setVisibility(View.GONE);
        ctrl_navilinedetail_add.setVisibility(View.VISIBLE);
        lv_topbarMenu.setVisibility(View.VISIBLE);
    }

    public void onOrderCancel(View v) {
        mIsEditB = false;
        findViewById(R.id.ctrl_navilinedetail_order).setVisibility(View.GONE);
        ctrl_navilinedetail_add.setVisibility(View.VISIBLE);
        lv_topbarMenu.setVisibility(View.VISIBLE);

        initData();
    }

    @Override
    public void finish() {
        super.finish();
    }

    public void onAddNavilineWp(View v) {
        if (mRoute.waypointList.size() > Route.MAX_WAYPOINT) {
            showShortToast(getString(R.string.hxzdkybc) + Route.MAX_WAYPOINT + getString(R.string.ghd));
        }
        DBDataCenter.getInstance().routeShow = mRoute;
        toActivity(SearchTypeActivity.createIntentfromNaviLine(this), REQUEST_TO_SEARCH);
    }

    private void editName(boolean toWindow) {
        if (toWindow) {
            intent = EditTextInfoWindow.createIntent(context, EditTextInfoWindow.TYPE_NICK
                    , getString(R.string.hxmc), StringUtil.getTrimedString(mRoute.getName()), getPackageName());
        } else {
            intent = EditTextInfoActivity.createIntent(context, EditTextInfoActivity.TYPE_NICK
                    , getString(R.string.hxmc), StringUtil.getTrimedString(mRoute.getName()));
        }

        toActivity(intent, REQUEST_TO_EDIT_TEXT_INFO, !toWindow);
    }

    private void onMap() {
        if (mRoute.waypointList == null) {
            mRoute.waypointList = new ArrayList<WayPoint>();
        }
        if (mRoute.waypointList.size() < 1) {
            showShortToast(getString(R.string.hxswhdqtjhd));
            return;
        }
        DBDataCenter.getInstance().routeShow = mRoute;

        toActivity(DMapActivity.createIntentFromRoute(context, true), REQUEST_TO_ONMAP);
    }

    private void onNavi() {
        if (mRoute.waypointList.size() < 1) {
            showShortToast(getString(R.string.dhdhxzsxyyghdqtjhd));
        }

        if (DBDataCenter.getInstance().routeNavi != null) {
            new AlertDialog(context, "", getString(R.string.qdzzdqhxrhaxhxdh), true, 11, this).show();
        } else if (DBDataCenter.getInstance().wayTrailNavi != null) {
            new AlertDialog(context, "", getString(R.string.qdzzdqhjrhaxhxdh), true, 11, this).show();
        } else if (Start.getInstance().isHavePath()) {
            new AlertDialog(context, "", getString(R.string.qdzzdqdhrhksxddh), true, 11, this).show();
        } else {
            DBDataCenter.getInstance().makerNavi = null;
            DBDataCenter.getInstance().wayTrailNavi= null;

            DBDataCenter.getInstance().routeNavi = mRoute;
            DBDataCenter.getInstance().routeNaviIndex = 0;
            SysUtils.getInstance().startRouteNavi();
            Start.getInstance().setHavePath(false);
            setResult(Msg.NAVI_BY_ROUTE);
            finish();
        }
    }

    private void onNaviBack() {
        if (mRoute.waypointList.size() < 1) {
            showShortToast(getString(R.string.dhdhxzsxyyghdqtjhd));
        }

        if (DBDataCenter.getInstance().routeNavi != null) {
            new AlertDialog(context, "", getString(R.string.qdzzdqhxrhaxhxdh), true, 12, this).show();
        } else if (DBDataCenter.getInstance().wayTrailNavi != null) {
            new AlertDialog(context, "", getString(R.string.qdzzdqhjrhaxhxdh), true, 12, this).show();
        }  else if (Start.getInstance().isHavePath()) {
            new AlertDialog(context, "", getString(R.string.qdzzdqdhrhksxddh), true, 12, this).show();
        } else {
            DBDataCenter.getInstance().makerNavi = null;
            DBDataCenter.getInstance().wayTrailNavi= null;

            DBDataCenter.getInstance().routeNavi = new Route();
            for (int i = 0; i < mRoute.waypointList.size(); i++) {
                DBDataCenter.getInstance().routeNavi.waypointList.add(0, mRoute.waypointList.get(i));
            }
            DBDataCenter.getInstance().routeNaviIndex = 0;
            SysUtils.getInstance().startRouteNavi();
            Start.getInstance().setHavePath(false);
            setResult(Msg.NAVI_BY_ROUTE);
            finish();
        }
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

    private final int REQUEST_TO_EDIT_TEXT_INFO = 23;
    private final int REQUEST_TO_ONMAP = 26;
    private final int REQUEST_TO_LISTORDER = 27;
    private final int REQUEST_TO_TOP_MENU = 30;
    private final int REQUEST_TO_SEARCH = 32;
    private final int REQUEST_TO_EDIT_ORDER = 43;
    private final int REQUEST_DETAIL = 41;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Msg.NAVI_BY_ROUTE) {
            setResult(Msg.NAVI_BY_ROUTE);
            finish();
        }

        if (resultCode == Msg.NAVI_TO_DEST) {
            setResult(resultCode);
            finish();
            return;
        }

        if (requestCode == REQUEST_TO_ONMAP) {
            mRoute = DBDataCenter.getInstance().routeShow;
            DBDataCenter.getInstance().routeShow = null;
            initData();
            resetInfo();
            return;
        }

        if (requestCode == REQUEST_TO_LISTORDER) {
            changeOrderList(resultCode);
            return;
        }

        if (requestCode == REQUEST_DETAIL) {
            adapter.notifyDataSetChanged();
            return;
        }

        if (requestCode == REQUEST_TO_SEARCH) {
            if (resultCode == Msg.SEACHSELECTEDONE) {
                if (DBDataCenter.getInstance().makerSeach != null) {
                    WayPoint wp = DBDataCenter.getInstance().makerSeach;
                    wp.setId(null);//必须是一个新的航点
                    wp.setType(0);
                    wp.setRouteid(mRoute.getId());
                    Long wpId = DBDataCenter.getInstance().saveWayPoint(wp);
                    wp.setId(wpId);
                    String strWps = mRoute.getWaypoint();
                    if (strWps.length() < 1) {
                        strWps = wp.getId() + "";
                    } else {
                        strWps += "-" + wp.getId();
                    }
                    mRoute.setWaypoint(strWps);
                    DBDataCenter.getInstance().saveRoute(mRoute);
                    initData();
                    DBDataCenter.getInstance().makerSeach = null;
                } else {
                    mRoute = DBDataCenter.getInstance().routeShow;
                    DBDataCenter.getInstance().routeShow = null;
                    initData();
                    resetInfo();
                }
                return;
            }
        }

        if (resultCode != RESULT_OK) {
            return;
        }


        switch (requestCode) {
            case REQUEST_TO_EDIT_ORDER:
                if (data != null) {
                    String editedValue = data.getStringExtra(EditTextInfoWindow.RESULT_VALUE);
                    int order = Integer.valueOf(editedValue);
                    if (order < 1 || order > navilinelist.size()) {
                        Toast.makeText(this, getString(R.string.qsryxdzy) + navilinelist.size() + ")", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    order--;
                    changeOrderList(order);
                }
                break;
            case REQUEST_TO_EDIT_TEXT_INFO:
                if (data != null) {
                    String name = StringUtil.getTrimedString(
                            data.getStringExtra(EditTextInfoWindow.RESULT_VALUE));
                    if (name.length() < 1) {
                        showShortToast(getString(R.string.hxmcbnwk));
                        return;
                    }
                    mRoute.setName(name);
                    DBDataCenter.getInstance().saveRoute(mRoute);
                    tvBaseTitle.setText(name);
                }
                break;
            case REQUEST_TO_TOP_MENU:
                if (data != null) {
                    switch (data.getIntExtra(TopMenuWindow.RESULT_POSITION, -1)) {
                        case 0:
                            editName(true);
                            break;
                        case 1:
                            onToEdit();
                            break;
                        case 2:
                            onToEditB();
                            break;
                        case 3:
                            if (navilinelist.size() > 0) {
                                new AlertDialog(context, getString(R.string.wxts), getString(R.string.qdqkhd), true, 1, this).show();
                            } else {
                                showShortToast(getString(R.string.swjl));
                            }
                            break;
                        case 4:
                            onMap();
                            break;
                        case 5:
                            onNavi();
                            break;
                        case 6:
                            onNaviBack();
                            break;
                        default:
                            break;
                    }
                }
                break;
        }
    }

    @Override
    public void onDialogButtonClick(int requestCode, boolean isPositive) {
        if (isPositive == false) {
            return;
        }

        if (requestCode == 0) {
            delSelected();
        } else if (requestCode == 1) {
            clearListData();
        } else if (requestCode == 11) {

            Start.getInstance().MapKit().clearRoute();
            Start.getInstance().MapKit().clearNavStart();
            Start.getInstance().MapKit().mNavDest = null;
            Start.getInstance().setHavePath(false);

            DBDataCenter.getInstance().makerNavi = null;
            DBDataCenter.getInstance().wayTrailNavi = null;

            DBDataCenter.getInstance().routeNavi = mRoute;
            DBDataCenter.getInstance().routeNaviIndex = 0;
            SysUtils.getInstance().startRouteNavi();
            Start.getInstance().setHavePath(false);
            setResult(Msg.NAVI_BY_ROUTE);
            finish();
        } else if (requestCode == 12) {
            DBDataCenter.getInstance().routeNavi = null;
            DBDataCenter.getInstance().wayTrailNavi = null;
            Start.getInstance().setHavePath(false);

            Route routeNavi = new Route();
            for (int i = 0; i < mRoute.waypointList.size(); i++) {
                routeNavi.waypointList.add(0, mRoute.waypointList.get(i));
            }
            DBDataCenter.getInstance().routeNavi = routeNavi;
            DBDataCenter.getInstance().routeNaviIndex = 0;
            SysUtils.getInstance().startRouteNavi();
            Start.getInstance().setHavePath(false);
            setResult(Msg.NAVI_BY_ROUTE);
            finish();
        }

    }

    private final int DIALOG_SET_TOPBAR = 1;

    @Override
    public void onDialogItemClick(int requestCode, int position, String item) {
        if (position < 0) {
            position = 0;
        }
        switch (requestCode) {
            case DIALOG_SET_TOPBAR:
                onEditBNext(position);
                break;
            default:
                break;
        }
    }


    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //Adapter
    public class DemoAdapter extends BaseAdapter<Entry<Integer, String>> {
        public DemoAdapter(Activity context) {
            super(context);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(
                        R.layout.navilinedetail_view, parent, false);
            }

            TextView iv_index = BaseViewHolder.get(convertView, R.id.iv_index);
            ImageView iv_icon = BaseViewHolder.get(convertView, R.id.iv_icon);
            TextView tv_naviptname = BaseViewHolder.get(convertView, R.id.tv_naviptname);
            TextView tv_naviptdistance = BaseViewHolder.get(convertView, R.id.tv_naviptdistance);
            TextView tv_naviptdir = BaseViewHolder.get(convertView, R.id.tv_naviptdir);
            TextView tv_navipttime = BaseViewHolder.get(convertView, R.id.tv_navipttime);
            ImageView iv_pic = BaseViewHolder.get(convertView, R.id.iv_pic);
            iv_pic.setVisibility(View.GONE);
            TextView iv_check = BaseViewHolder.get(convertView, R.id.iv_check);
            iv_check.setTypeface(AppContext.getInstances().getIconfont());
            ImageView ivrtimg = BaseViewHolder.get(convertView, R.id.ivrtimg);

            iv_index.setVisibility(View.GONE);

            final Entry<Integer, String> data = getItem(position);
            int index = data.getKey();
            if (index >= 0 && index < mRoute.waypointList.size()) {
                WayPoint navipt = mRoute.waypointList.get(index);
                iv_icon.setImageResource(navipt.getTypeIcon());
                tv_naviptname.setText(navipt.getName());
                if (navipt.getType() == 1) {
                    tv_naviptname.setTextColor(context.getResources().getColor(R.color.orangered));
                } else {
                    tv_naviptname.setTextColor(context.getResources().getColor(R.color.nhc_maintab_txt_normal));
                }
                LonLat llcar = SysUtils.getInstance().getLL();
                int distance = (int) Start.getInstance().MapKit().getDistance(llcar, navipt.getLL());
                String DIS[] = SysUtils.getInstance().getDistanceStr(distance);
                tv_naviptdistance.setText(DIS[0] + DIS[1]);
                tv_naviptdir.setText(Start.getInstance().MapKit().getDirection(llcar, navipt.getLL()));
                tv_navipttime.setText(SysUtils.getInstance().getDateStr(navipt.getDatetime()));
                if (navipt.getPicurl().length() > 0) {
                    iv_pic.setVisibility(View.VISIBLE);
                }

                iv_check.setVisibility(View.GONE);
                if (mIsEdit) {
                    ivrtimg.setVisibility(View.GONE);
                    boolean isSelected = hashMap.get(index);
                    iv_check.setVisibility(View.VISIBLE);
                    if (isSelected) {
                        iv_check.setText(R.string.fonticon_xz);
                    } else {
                        iv_check.setText(R.string.fonticon_wxz);
                    }
                } else if (mIsEditB) {
                    iv_index.setVisibility(View.VISIBLE);
                    iv_index.setText((new DecimalFormat("00").format(position + 1)));
                } else {
                    ivrtimg.setVisibility(View.VISIBLE);
                }
            }

            return super.getView(position, convertView, parent);
        }

        private final long serialVersionUID = 3L;

        @Override
        public long getItemId(int position) {
//            return getItem(position).getId();
            return serialVersionUID + position;
        }
    }

}